//
//  HMMainTabBarController.swift
//  sina
//
//  Created by 崔正强 on 17/3/19.
//  Copyright © 2017年 崔正强. All rights reserved.
//

import UIKit

class HMMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        let hmTabBar = HMTabBar()
        
        //监听按钮点击事件
        hmTabBar.clickClosure = {[weak self] (tabar: HMTabBar) -> () in
            
            print("加号按钮被点击了\(self)")
        }
        
        self.setValue(hmTabBar, forKey: "tabBar")
    }

    //添加子试图控制器
    private func addChildViewControllers() {
        
        //首页
        addChildViewController(vc: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        //消息
        addChildViewController(vc: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_message_center")
        //发现
        addChildViewController(vc: HMMessagerTableViewController(), title: "消息", imageName: "tabbar_discover")
        //我
        addChildViewController(vc: HMProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title: String,imageName: String){
    //单独设置文字在某些版本不能正常显示 必须同时设置image
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片 和选中的文字颜色
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        
        vc.tabBarItem.badgeValue = "10"
        vc.tabBarItem.badgeValue = nil
        vc.tabBarItem.badgeColor = UIColor.purple
        
        //包装到导航视图控制器
        let nav = HMBaseNavController(rootViewController: vc)
        
        //添加到tabbarController
        addChildViewController(nav)
    }

}
